using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UpdatingPictureBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // create the image
            this.mImage = new Bitmap(800, 600);
            
            // make the image a funky color
            Graphics graphics = Graphics.FromImage(this.mImage);
            graphics.Clear(Color.Chartreuse);
            graphics.Dispose();

            // assign this image to the picture box
            this.pictureBox1.Image = this.mImage;
        }

        Bitmap mImage;

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics graphics = Graphics.FromImage(this.mImage);

            Random random = new Random();

            for (int i = 0; i < 50; i++)
            {
                graphics.DrawImage(new Bitmap("../../sq1.jpg"), 
                    random.Next(0, this.mImage.Width),
                    random.Next(0, this.mImage.Height));
            }
            graphics.SetClip(new Rectangle(50, 150, 200, 200));
            graphics.Clear(Color.Black);
            graphics.Dispose();

            this.mImage.Save("sq1.bmp");

            // needed to reflect changes in the picture box
            this.pictureBox1.Invalidate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics graphics = Graphics.FromImage(this.mImage);

            Random random = new Random();

            for (int i = 0; i < 50; i++)
            {
                graphics.DrawImage(new Bitmap("../../sq2.jpg"),
                    random.Next(0, this.mImage.Width),
                    random.Next(0, this.mImage.Width));
            }

            graphics.Dispose();

            this.mImage.Save("sq2.bmp");

            // needed to reflect changes in the picture box
            this.pictureBox1.Invalidate();
        }
    }
}